import {
	mapState,
	mapMutations
} from 'vuex'
export default {
	data() {
		return {
			// customTip: {
			// 	notDetermined: '开启定位权限，花比三家为您提供配送服务。',
			// 	denied: '开启定位权限，花比三家为您提供配送服务。',
			// 	wxAppNotDenied: '微信未获得位置权限，请到设置中开启；开启定位，花比三家为您提供配送服务。',
			// 	notEnabled: '系统定位未开启，请前往手机系统设置中开启；开启定位，花比三家为您提供配送服务。'
			// },
			loactionTip: '',
			isOpenSeting: false,
			showLocation: false,
			showchangeLocation: false, //定位和选中城市不同时，提示
			loadAddr: false
		}
	},
	computed: {
		...mapState(['appName']),
		customTip() {
			return {
				notDetermined: '开启定位权限，' + this.appName + '为您提供配送服务。',
				denied: '开启定位权限，' + this.appName + '为您提供配送服务。',
				wxAppNotDenied: '微信未获得位置权限，请到设置中开启；开启定位，' + this.appName + '为您提供配送服务。',
				notEnabled: '系统定位未开启，请前往手机系统设置中开启；开启定位，' + this.appName + '为您提供配送服务。'
			}
		}
	},
	methods: {
		...mapMutations(['SET_CURRENTLOCATION', 'SET_LOCATIONCITY']),
		// 没有获取到位置，不停获取
		getLocationAuth() {
			var that = this;
			this.isOpenSeting = false;

			//wx.getSetting是获取用户授权的信息的，除了应用在位置信息授权还能应用在用户信息授权等等
			wx.getSetting({
				success: res => {
					// scope.userLocation  == undefined代表用户未授权且第一次登陆
					console.log('检查地理位置信息是否授权', res.authSetting['scope.userFuzzyLocation']);
					if (res.authSetting['scope.userFuzzyLocation'] == undefined) {
						console.log('hahah检查地理位置信息是否授权');
						that.getLoaction();
					}
					//小程序检测到用户不是第一次进入该页面,且未授权
					else if (res.authSetting['scope.userFuzzyLocation'] != undefined && res.authSetting[
							'scope.userFuzzyLocation'] != true) {
						that.isOpenSeting = true;
						that.loactionTip = 'denied';
					} else if (res.authSetting['scope.userFuzzyLocation'] == true) {
						//授权后默认加载，直接获取定位
						that.getLoaction();
					}
				}
			});
		},
		getLoaction() {
			var that = this;
			wx.getFuzzyLocation({
				type: 'gcj02',
				success: res => {
					console.log(res);
					this.isOpenSeting = false;
					//拿到用户当前位置的经纬度
					let latitude = res.latitude; //维度
					let longitude = res.longitude; //经度
					//换取地址
					uni.setStorageSync('latlng', res);
					this.getAddress(latitude, longitude);
				},
				fail: res => {
					this.isOpenSeting = true;
					this.loadAddr = false;
					console.log('res.errMsg', res.errMsg);
					if (res.errMsg === 'getFuzzyLocation:fail:auth denied') {
						that.loactionTip = 'denied';
						return;
					} else if (res.errMsg === 'getFuzzyLocation:fail auth deny') {
						this.showLocation = true;
					}
					that.loactionTip = 'denied';
					// this.loactionTip = 'notEnabled';
				}
			});
		},
		getAddress(latitude, longitude) {
			this.$postAction(this.$api.get_address, {
				location: longitude + ',' + latitude
			}).then(da => {
				if (da.code == 0) {
					this.loadAddr = false;
					this.SET_LOCATIONCITY(da.data);
					if (this.currentLocation && this.currentLocation != da
						.data.city) {
						this.showchangeLocation = true;
					} else {
						this.SET_CURRENTLOCATION(da.data);
					}
				}
			});
		},
		changeAddr() {
			let info = uni.getStorageSync('location');
			this.SET_CURRENTLOCATION(info);
			this.showchangeLocation = false;
		}

	}
}